package nbn.android.myyoutubeclient.adapter;

import java.util.ArrayList;

import nbn.android.myyoutubeclient.R;
import nbn.android.myyoutubeclient.object.VideoObject;
import nbn.android.myyoutubeclient.util.BaseEngine;
import nbn.android.myyoutubeclient.util.UtilMusic;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

@SuppressLint("InflateParams") public class VideoAdapter extends BaseAdapter {

	Context context;
	ArrayList<VideoObject> listPlaylist;

	public VideoAdapter(Context context, ArrayList<VideoObject> listPlaylist) {
		this.context = context;
		this.listPlaylist = listPlaylist;
	}

	@Override
	public int getCount() {
		return listPlaylist.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.video_item, null);
			holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressBarVideo_item);
			holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
			holder.imageView = (ImageView) convertView.findViewById(R.id.videoThumbnail);
			holder.textView = (TextView) convertView.findViewById(R.id.textviewVideoTitle);
			holder.textViewViewed = (TextView) convertView.findViewById(R.id.textViewViewed);
			holder.textViewDescription = (TextView) convertView.findViewById(R.id.textViewDuration);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		VideoObject videoObj = listPlaylist.get(position);
		Picasso.with(context).load(videoObj.getthumbnail()).into(holder.imageView, new Callback() {
			@Override
			public void onSuccess() {
				holder.progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onError() {
				holder.progressBar.setVisibility(View.GONE);
			}
		});
		if(videoObj.gettitle()!=null)
			holder.textView.setText(videoObj.gettitle());
//			holder.textView.setText((position+1)+". "+videoObj.gettitle());
		if(videoObj.getrate()!=null&&!videoObj.getrate().equals(""))
			holder.ratingBar.setRating(Float.parseFloat(videoObj.getrate()));
		else
			holder.ratingBar.setVisibility(View.GONE);
		if(videoObj.getview()!=null&&!videoObj.getview().equals("")){
			holder.textViewViewed.setText(BaseEngine.numbershowdetails(videoObj.getview())+" views");
		}
		else
			holder.textViewViewed.setVisibility(View.GONE);
		if(videoObj.getDuration()!=null&&!videoObj.getDuration().equals(""))
			holder.textViewDescription.setText(UtilMusic.SecondsToTimer(Integer.parseInt(videoObj.getDuration())));
		else
			holder.textViewDescription.setVisibility(View.GONE);
		return convertView;
	}

	private class ViewHolder {
		RatingBar ratingBar;
		ImageView imageView;
		TextView textView,textViewViewed;
		TextView textViewDescription;
		ProgressBar progressBar;
	}
}